﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineCourse.Model;
using OnlineCourse.Model.Entity;
using OnlineCourse.Services.IServices;
using OnlineCourse.Services.Model;

namespace OnlineCourse.Services
{
    public class CouponService : ICouponService
    {

        private ApplicationDbContext _db;
        public CouponService(ApplicationDbContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 添加优惠券
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Price"></param>
        /// <param name="Number"></param>
        /// <param name="Source"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public bool Add(string Title, decimal Price, int Number)
        {
            try
            {
                Coupon c = new Coupon
                {
                    Title = Title,
                    Price = Price,
                    Number = Number,
                };
                _db.Coupons.Add(c);
                _db.SaveChanges();
                return true;
            }
            catch (Exception error)
            {
                throw error;
            }
        }
        /// <summary>
        /// 删除优惠券
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(Guid id)
        {
            try
            {
                var a = _db.Coupons.FirstOrDefault(p => p.Id == id);
                if (a != null)
                {
                    _db.Coupons.Remove(a);
                    _db.SaveChanges();
                }
                return true;
            }
            catch (Exception error)
            {
                throw error;
            }
        }
        /// <summary>
        /// 单个优惠券明细
        /// </summary>
        /// <param name = "id" >优惠券ID</ param >
        /// < returns ></ returns >
        public CouponsModel Get(Guid id)
        {
            CouponsModel c = new CouponsModel();
            c = _db.CouponsRecords.Where(p => p.CouponId == id).Select(g => (new CouponsModel() { CouponId = g.CouponId, Number = g.Coupon.Number, Price = g.Coupon.Price, Title = g.Coupon.Title, CreateDate = g.CreatedDate, Source = g.Source, State = g.State, UserId = g.UserId, UpdatedDate = g.UpdatedDate })).FirstOrDefault();
            return c;
        }
        /// <summary>
        /// 增加优惠券张数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public bool Ad(Guid id, int number)
        {
            try
            {
                Coupon c = new Coupon();
                c = _db.Coupons.FirstOrDefault(p => p.Id == id);
                if (c != null)
                {
                    c.Number = c.Number+number;
                    c.UpdatedDate = DateTime.Now;
                    _db.SaveChanges();
                }
                return true;
            }
            catch (Exception error)
            {

                throw error;
            }
        }
        /// <summary>
        /// 销毁券张数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public bool Reduce(Guid id, int number)
        {
            try
            {
                Coupon c = new Coupon();
                c = _db.Coupons.FirstOrDefault(p => p.Id == id);
                if (c != null)
                {
                    c.Number = c.Number -number;
                    c.UpdatedDate = DateTime.Now;
                    _db.SaveChanges();
                }
                return true;
            }
            catch (Exception error)
            {

                throw error;
            }
        }
    }
}
